package model

import (
	"tushare/model/mysql"
)

type TushareStock struct {
	Id         int64   `json:"id"`
	TsCode     string  `json:"ts_code"`
	Symbol     string  `json:"symbol"`
	Name       string  `json:"name"`
	Area       string  `json:"area"`
	Industry   string  `json:"industry"`
	Fullname   string  `json:"fullname"`
	Enname     string  `json:"enname"`
	Cnspell    string  `json:"cnspell"`
	Market     string  `json:"market"`
	Exchange   string  `json:"exchange"`
	CurrType   string  `json:"curr_type"`
	ListStatus string  `json:"list_status"`
	ListDate   string  `json:"list_date"`
	DelistDate string  `json:"delist_date"`
	IsHs       string  `json:"is_hs"`
	ImAmount   float64 `json:"im_amount"`
	Close      float64 `json:"close"`
	DayJetton  float64 `json:"day_jetton"`
	Collect    string  `json:"collect"`
	PeakButton string  `json:"peak_button"`
	PeakCenter string  `json:"peak_center"`
	PeakStable string  `json:"peak_stable"`
}

func StockCreate(ts TushareStock) {
	mysql.DB.Table("tushare_stock").Create(&ts)
}

func StockList() []TushareStock {
	ts := []TushareStock{}
	mysql.DB.Table("tushare_stock").Find(&ts)
	return ts
}

func StockListSimpPage(page, pageSize int64) []TushareStock {
	page = page
	pageSize = pageSize
	offset := (page - 1) * pageSize
	ts := []TushareStock{}
	mysql.DB.Table("tushare_stock")
	table := mysql.DB.Table("tushare_stock")
	table = table.Limit(pageSize).Offset(offset).Find(&ts)
	return ts
}

func StockListPage(page, pageSize int64, tsCode string, collect int64, mapType string, closeAmount float64, name string) []TushareStock {
	page = page
	pageSize = pageSize
	offset := (page - 1) * pageSize
	ts := []TushareStock{}
	table := mysql.DB.Table("tushare_stock")
	if tsCode != "" {
		table = table.Where("symbol = ?", tsCode)
	}
	if name != "" {
		table = table.Where("name like ?", "%"+name+"%")
	}
	if collect == 1 && mapType == "collect" {
		table = table.Where("collect = ?", collect)
	}
	if collect == 1 && mapType == "peakButton" {
		table = table.Where("peak_button = ?", collect)
	}
	if collect == 1 && mapType == "peakCenter" {
		table = table.Where("peak_center = ?", collect)
	}
	if collect == 1 && mapType == "peakStable" {
		table = table.Where("peak_stable = ?", collect)
	}
	table = table.Where("close <= ?", closeAmount)
	table = table.Order("close desc").Limit(pageSize).Offset(offset).Find(&ts)
	return ts
}

func StockCount(tsCode string, collect int64, mapType string, closeAmount float64, name string) int64 {
	var count int64
	table := mysql.DB.Table("tushare_stock")
	if tsCode != "" {
		table = table.Where("symbol = ?", tsCode)
	}
	if name != "" {
		table = table.Where("name like ?", "%"+name+"%")
	}
	if collect == 1 && mapType == "collect" {
		table = table.Where("collect = ?", collect)
	}
	if collect == 1 && mapType == "peakButton" {
		table = table.Where("peak_button = ?", collect)
	}
	if collect == 1 && mapType == "peakCenter" {
		table = table.Where("peak_center = ?", collect)
	}
	if collect == 1 && mapType == "peakStable" {
		table = table.Where("peak_stable = ?", collect)
	}
	table = table.Where("close <= ?", closeAmount)
	table = table.Count(&count)
	return count
}

func StockUpdate(tsCode string, stock TushareStock) {
	mysql.DB.Table("tushare_stock").Where("ts_code = ?", tsCode).Update(&stock)
}

func StockExec(sql string) {
	mysql.DB.Table("tushare_stock").Exec(sql)
}

func StockRiseFall() (int64, int64, int64) {
	var riseCount int64
	mysql.DB.Table("tushare_stock").Where("im_amount > 0").Count(&riseCount)

	var fallCount int64
	mysql.DB.Table("tushare_stock").Where("im_amount < 0").Count(&fallCount)

	var count int64
	mysql.DB.Table("tushare_stock").Where("im_amount = 0").Count(&count)

	return riseCount, fallCount, count
}

func StockListCollect() []TushareStock {
	ts := []TushareStock{}
	table := mysql.DB.Table("tushare_stock").Where("collect = ?", 1)
	table = table.Or("peak_button = ?", 1).Or("peak_center = ?", 1).Or("peak_stable = ?", 1)
	table = table.Find(&ts)
	return ts
}

/**
获取接近底部价格
**/
func GetButtonClose(diffValue float64) []TushareStock {
	ts := []TushareStock{}
	mysql.DB.Table("tushare_stock").Where("day_jetton > ?", 0).Where("close - day_jetton <= ?", diffValue).Find(&ts)
	return ts
}

func StockInfoByTsCode(tsCode string) TushareStock {
	ts := TushareStock{}
	mysql.DB.Table("tushare_stock").Where("ts_code =  ?", tsCode).Scan(&ts)
	return ts
}
